package com.itcc.controller;

import com.itcc.domain.Customer;
import com.itcc.domain.CustomerInfo;
import com.itcc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @PostMapping    //注册
    public Result pojoData(@RequestBody Customer customer){
        System.out.println(customer);
        CustomerInfo customerInfo = new CustomerInfo(customer.getCtm_name(), customer.getProfile(), customer.getDocx());
        System.out.println(customerInfo);
        if(customer.getCtm_name().isEmpty() || customer.getCtm_tel().isEmpty()){
            return new Result(Code.REGISTER_ERR);
        }else{
            boolean flag = customerService.inertCustomer(customer);
            boolean flag2 = customerService.insertCustomer_info(customerInfo);
            return new Result(flag && flag2 ? Code.REGISTER_OK : Code.REGISTER_ERR);
        }
    }

    @GetMapping("/{ctm_name}/{ctm_tel}")    //登录
    public Result setCustomer(@PathVariable String ctm_name, @PathVariable String ctm_tel){
        boolean flag = ctm_tel.equals(customerService.selectTelByName(ctm_name));
        Integer code = flag ? Code.LOGON_OK : Code.LOGON_ERR;
        String str = customerService.selectProfile(ctm_name);
        return new Result(code, str);
    }

//    @GetMapping
//    public ModelAndView customerData(){
//        ModelAndView bookModeView = new ModelAndView("redirect:/pages/books.jsp");
//        ModelAndView customerModeView = new ModelAndView("redirect:/customer.jsp");
//        return this.flag ? bookModeView : customerModeView;
//    }

}
